const database = require("../app/database");

class EssayServices {
  //获取一定数量的文章
  async getallessay(start, size, pass) {
    const str = `
			select *,
					(select count(*) from collection where essay_id=essay.id) as 'collectcount',
					(select count(*) from comments where essay_id = essay.id) as 'pingluncount'
			from essay where pass=? limit ?,?;
		`;
    const str1 = `
      select count(*) count from essay where pass=?
    `;
    const [result] = await database.execute(str, [pass, start, size]);
    const [[count]] = await database.execute(str1, [pass]);
    return {
      status: 200,
      result,
      count: count.count,
    };
  }

  //以不同的形式获取文章：1:获取最新的文章 2:获取浏览量最高的文章
  async getessayanyway(type) {
    let str, result;
    switch (type) {
      case "1":
        str = `
          select * from essay order by updateAt limit 0,3;
        `;
        [result] = await database.execute(str);
        break;
      case "2":
        str = `
          select * from essay order by views limit 0,3;
        `;
        [result] = await database.execute(str);
        break;
    }
    return {
      status: 200,
      result,
    };
  }

  //获取文章详情
  async getessaydetail(id) {
    const str = `
      select *,(select JSON_OBJECT('username',username,'id',user.id,'avatar',user.avatar) from user where id=e.user_id) author,
        (select label.content from label where id=e.label_id) label,    
        (select content from label where id=e.label_id) labelname,
        (select count(*) from collection where essay_id=e.id) collectcount
      from essay e where id=?;
    `;
    const [[result]] = await database.execute(str, [id]);
    return {
      status: 200,
      result,
    };
  }

  //收藏文章或者取消收藏
  async changcollect(body) {
    const { userid, essayid } = body;
    const str1 = `
      select * from collection where user_id=? and essay_id=?
    `;
    const [re1] = await database.execute(str1, [userid, essayid]);

    if (re1.length > 0) {
      const str = `
        delete from collection where user_id=? and essay_id=?
      `;
      await database.execute(str, [userid, essayid]);
      return {
        status: 200,
        result: {
          message: "取消收藏成功",
        },
      };
    } else {
      const str = `
        insert into collection(user_id,essay_id) values(?,?)
      `;
      await database.execute(str, [userid, essayid]);
      return {
        status: 200,
        result: {
          message: "收藏文章成功",
        },
      };
    }
  }

  //增加浏览量
  async addviews(id) {
    const str = `
      update essay set views=views+1 where id=?;
    `;
    await database.execute(str, [id]);
    return {
      status: 200,
      result: "成功",
    };
  }

  //根据标题或内容文章
  async search(content) {
    const str = `
      select title,content,id from essay where title like '%${content}%' or content like '%${content}%';
    `;
    const [result] = await database.execute(str);
    return {
      result,
      status: 200,
    };
  }

  //发表文章
  async publish(body) {
    const { content, user_id, label_id, title, cover } = body;
    const str = `
      insert into essay(title,content,cover,label_id,user_id) values(?,?,?,?,?)
    `;
    await database.execute(str, [title, content, cover, label_id, user_id]);
    return {
      result: {
        message: "发表文章成功",
      },
      status: 200,
    };
  }

  //通过用户id去查询发表的或者收藏的文章
  async getbyuser(id, type) {
    if (type === "0") {
      //查询收藏的文章
      const str = `
        select *,
            (select count(*) from collection where essay_id=essay.id) as 'collectcount',
            (select count(*) from comments where essay_id = essay.id) as 'pingluncount'
        from essay where id in (select essay_id from collection where user_id=?);
      `;
      const [result] = await database.execute(str, [id]);
      return {
        status: 200,
        result,
      };
    } else {
      //查询发表的文章
      const str = `
        select *,
            (select count(*) from collection where essay_id=essay.id) as 'collectcount',
            (select count(*) from comments where essay_id = essay.id) as 'pingluncount'
        from essay where user_id=?;
      `;
      const [result] = await database.execute(str, [id]);
      return {
        status: 200,
        result,
      };
    }
  }

  //删除指定文章
  async deleteessaybyid(id) {
    const str = `
      delete from essay where id=?
    `;
    await database.execute(str, [id]);
    return {
      status: 200,
      result: {
        message: "删除成功",
      },
    };
  }

  //管理员获取所有文章
  async mangetallessay(start, size, search, starttime, endtime) {
    const str = `
      select * from essay where createAt<? and createAt>? and (title like '%${search}%' or content like '%${search}%') limit ?,?
    `;
    const [result] = await database.execute(str, [
      endtime,
      starttime,
      start,
      size,
    ]);
    const [re] = await database.execute(
      `select * from essay where createAt<? and createAt>? and (title like '%${search}%' or content like '%${search}%')`,
      [endtime, starttime]
    );
    return {
      status: 200,
      result,
      total: re.length,
    };
  }

  //根据字段audit查询未读的文章消息的数量
  async getnotreadycount(userid) {
    const str = `
      select count(*) count from essay where user_id=? and audit=2
    `;
    const [[{ count }]] = await database.execute(str, [userid]);
    return {
      status: 200,
      result: count,
    };
  }

  //查询当前登录用户的所有文章消息
  async getallnotice(userid) {
    const str = `
      select * from essay where user_id=? order by createAt desc
    `;
    const [result] = await database.execute(str, [userid]);
    return {
      status: 200,
      result,
    };
  }

  //置所有消息为已读
  async toreaded(userid) {
    const str = `
      update essay set audit=3 where user_id=? and audit=2
    `;
    await database.execute(str, [userid]);
    return {
      status: 200,
      result: {
        message: "成功",
      },
    };
  }

  //获取所有需要审核的文章
  async allchecke(start, size, search) {
    const str = `
      select * from essay where audit=1 and title like '%${search}%' limit ?,?
    `;
    const [result] = await database.execute(str, [start, size]);
    const [re] = await database.execute(
      `select * from essay where audit=1 and title like '%${search}%'`
    );
    return {
      status: 200,
      result,
      total: re.length,
    };
  }

  //改变文章审核状态
  async changestate(essayid, state) {
    const str = `
      update essay set pass=?,audit=? where id=?
    `;
    await database.execute(str, [state, state === 1 ? 3 : 2, essayid]);
    return {
      status: 200,
      result: {
        message: "修改成功",
      },
    };
  }
}

module.exports = new EssayServices();
